package com.yellowpage.omc.canal.action;
  
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.omc.canal.data.Canal;
import com.yellowpage.omc.canal.service.CanalService;

public class CanalAction extends BaseAction {

    public String RESULT_QUERY = "query";
    public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	
	private Configuration configuration;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
	
	private List<Canal> canalList = new ArrayList<Canal>();
	private List<Map> resultList = new ArrayList<Map>();
	
	private Canal canal;
	private CanalService canalService;
	
	private String selectedId;	
	private String[] selectedIds;	
	private String message;
	
		private String query_canalId;
		private String query_abbr;
		private String query_promotionWay;
		private String query_contactName;
		private String query_contactTel;
		private String query_email;
		private String query_createUser;
		private String query_createTime;
		private String query_lastUpdateUser;
		private String query_lastUpdateTime;
		private String query_note;
		private String query_address;
		private String query_name;
		private String query_type;
		private String query_status;
		private String query_code;
		
	public void prepare() {
		super.prepare();
	
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.canal = canalService.getCanal(Long.parseLong(selectedId));
		}
	}
	
	public String queryByPage() {		
		initPageRender();
		
		Map queryMap = new HashMap();	
			    queryMap.put("canalId", query_canalId);
	    	    queryMap.put("abbr", query_abbr);
	    	    queryMap.put("promotionWay", query_promotionWay);
	    	    queryMap.put("contactName", query_contactName);
	    	    queryMap.put("contactTel", query_contactTel);
	    	    queryMap.put("email", query_email);
	    	    queryMap.put("createUser", query_createUser);
	    	    queryMap.put("createTime", query_createTime);
	    	    queryMap.put("lastUpdateUser", query_lastUpdateUser);
	    	    queryMap.put("lastUpdateTime", query_lastUpdateTime);
	    	    queryMap.put("note", query_note);
	    	    queryMap.put("address", query_address);
	    	    queryMap.put("name", query_name);
	    	    queryMap.put("type", query_type);
	    	    queryMap.put("status", query_status);
	    	    queryMap.put("code", query_code);
	    	    	
		resultList = canalService.queryCanalByPage(queryMap, pageRender);
		
		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	
	public String toAdd() {
		return RESULT_ADD;
	}
	
	public String save() {
	    
		canalService.saveCanal(canal);		
		
		message = "新增成功!";
		return RESULT_ADD;
	}
	
	public void validateSave() {
	    	    	    	    	    if (canal.getAbbr().length() == 0) {
			this.addActionError("** Abbr 不能为空 !");
		}
			    	    	    if (canal.getPromotionWay().length() == 0) {
			this.addActionError("** PromotionWay 不能为空 !");
		}
			    	    	    	    	    if (canal.getContactTel().length() == 0) {
			this.addActionError("** ContactTel 不能为空 !");
		}
			    	    	    if (canal.getEmail().length() == 0) {
			this.addActionError("** Email 不能为空 !");
		}
			
			    	    	    if (canal.getAddress().length() == 0) {
			this.addActionError("** 简称 不能为空 !");
		}
			    	    	    if (canal.getName().length() == 0) {
			this.addActionError("** 名称 不能为空 !");
		}
			    	    	    if (canal.getType().length() == 0) {
			this.addActionError("** 类型 不能为空 !");
		}
			    	    	    if (canal.getStatus().length() == 0) {
			this.addActionError("** 状态 不能为空 !");
		}
			    	    	    if (canal.getCode().length() == 0) {
			this.addActionError("** 编号 不能为空 !");
		}
	    if (canal.getContactName().length() == 0) {
			this.addActionError("** 联系人 不能为空 !");
		}
			    	    
		this.setValidatorResult("/omc/canal/CanalAdd.jsp");		
	}
	
	public String toEdit() {
	    Long id = new Long(selectedId);
	    
	    canal = canalService.getCanal(id);
	
		return RESULT_EDIT;
	}
	
	public String update() {
	
	    canalService.updateCanal(canal);	

		message = "修改成功!";
	    
		return RESULT_EDIT;
	}
	
	public void validateUpdate() {
					    			    if (canal.getAbbr().length() == 0) {
			this.addActionError("** Abbr 不能为空 !");
		}
			    			    if (canal.getPromotionWay().length() == 0) {
			this.addActionError("** PromotionWay 不能为空 !");
		}
			    			    			    if (canal.getContactTel().length() == 0) {
			this.addActionError("** ContactTel 不能为空 !");
		}
			    			    if (canal.getEmail().length() == 0) {
			this.addActionError("** Email 不能为空 !");
		}
			    			    			    			    			    			    if (canal.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
			    			    if (canal.getAddress().length() == 0) {
			this.addActionError("** Address 不能为空 !");
		}
			    			    if (canal.getName().length() == 0) {
			this.addActionError("** Name 不能为空 !");
		}
			    			    if (canal.getType().length() == 0) {
			this.addActionError("** Type 不能为空 !");
		}
			    			    if (canal.getStatus().length() == 0) {
			this.addActionError("** Status 不能为空 !");
		}
			    			    if (canal.getCode().length() == 0) {
			this.addActionError("** Code 不能为空 !");
		}
			    		this.setValidatorResult("/omc/canal/CanalUpdate.jsp");	
	}
	
	public String delete() {	    
	    List ids = new ArrayList();
	    
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		
		try {
			this.canalService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲突删除失败 !");
		}
		
		
		return queryByPage();
	}	
	
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	 public void setCanal(Canal canal) {
		this.canal = canal;
	}
	
	public Canal getCanal() {
		return canal;
	}
	 
	 
	 public void setCanalService(CanalService canalService) {
		this.canalService = canalService;
	}
	
	public List<Canal> getCanalList() {
		return canalList;
	}

	public void setCanalList(List<Canal> canalList) {
		this.canalList = canalList;
	}
	
	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}
	
	    public String getQuery_canalId() {
        return this.query_canalId;
    }
    
    public void setQuery_canalId(String canalId){
        this.query_canalId = canalId;
    }    

        public String getQuery_abbr() {
        return this.query_abbr;
    }
    
    public void setQuery_abbr(String abbr){
        this.query_abbr = abbr;
    }    

        public String getQuery_promotionWay() {
        return this.query_promotionWay;
    }
    
    public void setQuery_promotionWay(String promotionWay){
        this.query_promotionWay = promotionWay;
    }    

        public String getQuery_contactName() {
        return this.query_contactName;
    }
    
    public void setQuery_contactName(String contactName){
        this.query_contactName = contactName;
    }    

        public String getQuery_contactTel() {
        return this.query_contactTel;
    }
    
    public void setQuery_contactTel(String contactTel){
        this.query_contactTel = contactTel;
    }    

        public String getQuery_email() {
        return this.query_email;
    }
    
    public void setQuery_email(String email){
        this.query_email = email;
    }    

        public String getQuery_createUser() {
        return this.query_createUser;
    }
    
    public void setQuery_createUser(String createUser){
        this.query_createUser = createUser;
    }    

        public String getQuery_createTime() {
        return this.query_createTime;
    }
    
    public void setQuery_createTime(String createTime){
        this.query_createTime = createTime;
    }    

        public String getQuery_lastUpdateUser() {
        return this.query_lastUpdateUser;
    }
    
    public void setQuery_lastUpdateUser(String lastUpdateUser){
        this.query_lastUpdateUser = lastUpdateUser;
    }    

        public String getQuery_lastUpdateTime() {
        return this.query_lastUpdateTime;
    }
    
    public void setQuery_lastUpdateTime(String lastUpdateTime){
        this.query_lastUpdateTime = lastUpdateTime;
    }    

        public String getQuery_note() {
        return this.query_note;
    }
    
    public void setQuery_note(String note){
        this.query_note = note;
    }    

        public String getQuery_address() {
        return this.query_address;
    }
    
    public void setQuery_address(String address){
        this.query_address = address;
    }    

        public String getQuery_name() {
        return this.query_name;
    }
    
    public void setQuery_name(String name){
        this.query_name = name;
    }    

        public String getQuery_type() {
        return this.query_type;
    }
    
    public void setQuery_type(String type){
        this.query_type = type;
    }    

        public String getQuery_status() {
        return this.query_status;
    }
    
    public void setQuery_status(String status){
        this.query_status = status;
    }    

        public String getQuery_code() {
        return this.query_code;
    }
    
    public void setQuery_code(String code){
        this.query_code = code;
    }    

    
	
}
